package com.alex.database;

import com.alex.books.BookStore;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

/**
 * 
 * @author Alex.Lu
 * 
 */
public class BookDatabaseHelper  extends SQLiteOpenHelper{

	private static final int VERSION = 2;
	private static String DB_NAME = BookStore.getInstance().DBPath+"book.db";
	private static BookDatabaseHelper instance;
	
	public BookDatabaseHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}
	
	public static BookDatabaseHelper getInstance(Context context){
		if(instance == null){
			instance = new BookDatabaseHelper(context);
		}
		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		StringBuffer sbuf = new StringBuffer();
		sbuf.setLength(0);
		sbuf.append("CREATE TABLE ");
		sbuf.append(Book_TB.TABLE_NAME);
		sbuf.append(" (");
		sbuf.append(Book_TB._ID);
		sbuf.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		sbuf.append(Book_TB.BOOK_NAME);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_SERIAL);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_PATH);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_AUTHOER);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_WORDNUMBER);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_CHAPTERNUMBER);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_SOURCE);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_ABOUTAUTHOR);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_ABOUTBOOK);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_COVERURL);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_ATTACH1);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_ATTACH2);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOk_LASTTIME);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.BOOK_MARKSNUMBER);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.BOOK_MARK);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.BOOK_ISFIRSTREAD);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.BOOK_CHAPTER_INDEX);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_ID);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_INDEX);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_NAME);
		sbuf.append(" TEXT,");
		sbuf.append(Book_TB.CHAPTER_BEGIN);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_END);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_LENGTH);
		sbuf.append(" INTEGER");
		sbuf.append(");");
		db.execSQL(sbuf.toString());
		
		sbuf.setLength(0);
		sbuf.append("CREATE TABLE ");
		sbuf.append(Bookmark_TB.TABLE_NAME);
		sbuf.append(" (");
		sbuf.append(Bookmark_TB._ID);
		sbuf.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		sbuf.append(Bookmark_TB.BOOK_ID);
		sbuf.append(" INTEGER,");
		sbuf.append(Bookmark_TB.BOOKMARK_NAME);
		sbuf.append(" TEXT,");
		sbuf.append(Bookmark_TB.BOOkMARK_TIME);
		sbuf.append(" TEXT,");
		sbuf.append(Bookmark_TB.BOOKMARK_MARK);
		sbuf.append(" INTEGER,");
		sbuf.append(Bookmark_TB.BOOKMARK_INTRO);
		sbuf.append(" TEXT,");
		sbuf.append(Bookmark_TB.CHAPTER_ID);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_INDEX);
		sbuf.append(" INTEGER,");
		sbuf.append(Bookmark_TB.CHAPTER_NAME);
		sbuf.append(" TEXT,");
		sbuf.append(Bookmark_TB.CHAPTER_BEGIN);
		sbuf.append(" INTEGER,");
		sbuf.append(Bookmark_TB.CHAPTER_END);
		sbuf.append(" INTEGER,");
		sbuf.append(Bookmark_TB.CHAPTER_LENGTH);
		sbuf.append(" INTEGER");
		sbuf.append(");");
		db.execSQL(sbuf.toString());
		
		sbuf.setLength(0);
		sbuf.append("CREATE TABLE ");
		sbuf.append(Chapter_TB.TABLE_NAME);
		sbuf.append(" (");
		sbuf.append(Chapter_TB._ID);
		sbuf.append(" INTEGER PRIMARY KEY AUTOINCREMENT,");
		sbuf.append(Chapter_TB.BOOK_ID);
		sbuf.append(" INTEGER,");
		sbuf.append(Book_TB.CHAPTER_INDEX);
		sbuf.append(" INTEGER,");
		sbuf.append(Chapter_TB.CHAPTER_NAME);
		sbuf.append(" TEXT,");
		sbuf.append(Chapter_TB.CHAPTER_BEGINPOS);
		sbuf.append(" INTEGER,");
		sbuf.append(Chapter_TB.CHAPTER_ENDPOS);
		sbuf.append(" INTEGER,");
		sbuf.append(Chapter_TB.CHAPTER_LENGTH);
		sbuf.append(" INTEGER");
		sbuf.append(");");
		db.execSQL(sbuf.toString());
		
		sbuf.setLength(0);
		sbuf.append("create trigger tr_bookmarkscount1" +
				" after insert " +
				" on tb_bookmark " +
				" for each row begin update tb_books " +
				" set book_marksnumber =" +
				" ( select count(*) from tb_bookmark where book_id = new.book_id)" +
				" where _id = new.book_id;" +
				" end ;");
		
		db.execSQL(sbuf.toString());
		
		sbuf.setLength(0);
		sbuf.append("create trigger tr_bookmarkscount2" +
				" after update " +
				" on tb_bookmark " +
				" for each row begin update tb_books " +
				" set book_marksnumber =" +
				" ( select count(*) from tb_bookmark where book_id = new.book_id)" +
				" where _id = new.book_id;" +
				" end ;");
		
		db.execSQL(sbuf.toString());
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.d("onUpgrade", "oldVersion:"+oldVersion +" newVersion:"+newVersion);
		StringBuffer sbuf = new StringBuffer();
		sbuf.setLength(0);
		sbuf.append("ALTER TABLE ");
		sbuf.append(Book_TB.TABLE_NAME);
		sbuf.append(" ADD ");
		sbuf.append(Book_TB.BOOk_LASTTIME);
		sbuf.append(" INTEGER  default 0");
		db.execSQL(sbuf.toString());
	}
	
	/**
	 * 书本
	 */
	public static final class Book_TB implements BaseColumns, BookColumns{
		public static final String TABLE_NAME = "tb_books";
	}
	
	public interface BookColumns{
		/**
		 * 书本信息
		 */
		String BOOK_NAME = "book_name";
		String BOOK_SERIAL = "book_serial";
		String BOOK_PATH = "book_path";
		String BOOK_AUTHOER = "book_author";
		String BOOK_WORDNUMBER = "book_wordnumber";
		String BOOK_CHAPTERNUMBER = "book_chapternumber";
		String BOOK_SOURCE = "book_source";
		String BOOK_ABOUTAUTHOR = "book_aboutauthor";
		String BOOK_ABOUTBOOK = "book_aboutbook";
		String BOOK_COVERURL = "book_coverurl";
		String BOOK_ATTACH1 = "book_attach1";
		String BOOK_ATTACH2 = "book_attach2";
		String BOOk_LASTTIME = "book_lasttime";
		String BOOK_MARKSNUMBER = "book_marksnumber";
		/**
		 * 
		 */
		String BOOK_MARK = "mark";
		String BOOK_ISFIRSTREAD = "book_is_firstread";
		String BOOK_CHAPTER_INDEX = "book_chapter_index";
		String CHAPTER_ID = "chapter_id";
		String CHAPTER_INDEX = "chapter_index";
		String CHAPTER_NAME = "chapter_name";
		String CHAPTER_BEGIN = "chapter_beginpos";
		String CHAPTER_END = "chapter_endpos";
		String CHAPTER_LENGTH = "chapter_length";
		
	}
	
	/**
	 * 书签表
	 */
	public static final class Bookmark_TB implements BaseColumns, BookmarkColumns{
		final static String TABLE_NAME = "tb_bookmark";
	}
	
	/**
	 * 书签字段
	 */
	public interface BookmarkColumns{
		String BOOK_ID = "book_id";
		String BOOKMARK_NAME = "book_name";
		String BOOKMARK_MARK = "book_mark";
		String BOOKMARK_INTRO = "book_intro";
		String BOOkMARK_TIME = "book_time";
		String CHAPTER_ID = "chapter_id";
		String CHAPTER_INDEX = "chapter_index";
		String CHAPTER_NAME = "chapter_name";
		String CHAPTER_BEGIN = "chapter_beginpos";
		String CHAPTER_END = "chapter_endpos";
		String CHAPTER_LENGTH = "chapter_length";
	}
	
	/**
	 * 章节表
	 */
	public static final class Chapter_TB implements BaseColumns, ChapterColumns{
		public static final String TABLE_NAME = "tb_chapters";
	}
	
	/**
	 * 章节字段表
	 */
	public interface ChapterColumns{
		String BOOK_ID = "book_id";
		String CHAPTER_INDEX = "chapter_index";
		String CHAPTER_NAME = "chapter_name";
		String CHAPTER_BEGINPOS = "chapter_begin_pos";
		String CHAPTER_ENDPOS = "chapter_end_pos";
		String CHAPTER_LENGTH = "chapter_length";
	}
	
}
